package com.sinodata.bsm.cicp.util.telnet;

import java.io.IOException;

/**
 * Telnet 任务.
 * 
 * @author thomas.h.zhang
 */
public class TelnetTask extends Thread {
    public TelnetTask() {
    }

    public TelnetTask(Telnet telnet, String cmd) {
        this.setTelnet(telnet);
        this.setCmd(cmd);
    }

    public void run() {
        try {
            if (cmd != null && cmd.length() != 0)
                telnet.sendCmd(cmd);
            shellOut = telnet.recvResp();
            this.setComplete(true);
        } catch (IOException e) {
            this.setComplete(false);
            this.setError(true);
            this.setErrorMsg(e.getMessage());
        }
        this.setActive(false);
        return;
    }

    protected boolean active = true;
    protected boolean complete = false;
    protected boolean error = false;
    protected String errorMsg;
    protected String cmd;
    protected String shellOut; // Shell执行结果.
    protected Telnet telnet;

    public Telnet getTelnet() {
        return telnet;
    }

    public void setTelnet(Telnet telnet) {
        this.telnet = telnet;
    }

    public String getCmd() {
        return cmd;
    }

    public void setCmd(String cmd) {
        this.cmd = cmd;
    }

    public String getShellOut() {
        return shellOut;
    }

    public void setShellOut(String shellOut) {
        this.shellOut = shellOut;
    }

    /**
     * Checks if this TelnetTask is complete.
     */
    public boolean isComplete() {
        return complete;
    }

    public void setComplete(boolean complete) {
        this.complete = complete;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public boolean isError() {
        return error;
    }

    public void setError(boolean error) {
        this.error = error;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

}
